package cn.itsource.hrm.utli;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author
 * @version v1.0.0
 * @date 2020/05/18
 */
public class CodeGen {

    public static void main(String[] args) throws Exception {

        //加载属性文件
        final Properties properties = new Properties();
        properties.load(CodeGen.class.getResourceAsStream("/mybatis-generator-system.properties"));


        //代码生成器对象
        AutoGenerator generator = new AutoGenerator();

        //各种配置=========================
        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        //System.getProperty("user.dir") 项目的根目录 - 根模块
        final String projectPath = System.getProperty("user.dir") +properties.getProperty("modelPath");

        final String facadePath = System.getProperty("user.dir")+ properties.getProperty("facadePath");

        globalConfig.setOutputDir(projectPath + "/src/main/java");
        globalConfig.setAuthor("jinhui");
        globalConfig.setOpen(false);

        generator.setGlobalConfig(globalConfig);

        //数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(properties.getProperty("datasource.url"));
        dataSourceConfig.setDriverName(properties.getProperty("datasource.driverClass"));
        dataSourceConfig.setUsername(properties.getProperty("datasource.username"));
        dataSourceConfig.setPassword(properties.getProperty("datasource.password"));

        generator.setDataSource(dataSourceConfig);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(properties.getProperty("package.parent"));
        pc.setEntity("domain");

        generator.setPackageInfo(pc);

        //自定义模板或者代码的生成路径
        //mapper 应该不使用默认路径(java)，自定义配置，生成到resources中
        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 如果模板引擎是 velocity
        String templatePath = "/templates/mapper.xml.vm";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/src/main/resources"+ properties.getProperty("mapper.location") +
                        tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });

        //自定义domain的输出位置
        templatePath = "/templates/entity.java.vm";
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return facadePath + "/src/main/java"+ properties.getProperty("domain.location") +
                        tableInfo.getEntityName() + StringPool.DOT_JAVA;
            }
        });

        //自定义query的输出位置
        templatePath = "/templates/query.java.vm";
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return facadePath + "/src/main/java"+ properties.getProperty("query.location") +
                        tableInfo.getEntityName()+"Query" + StringPool.DOT_JAVA;
            }
        });


        cfg.setFileOutConfigList(focList);
        generator.setCfg(cfg);


        //我们设置了mapper.xml的自定义配置，需要管理mapper.xml的默认配置
        //只需要将模板设置为null就关闭了
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        templateConfig.setController("templates/controller.java.vm");
        templateConfig.setEntity(null);


        generator.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        //设置生成的表名
        strategy.setInclude(properties.getProperty("table.names").split(","));
        //表前缀
        strategy.setTablePrefix(properties.getProperty("table.prefix"));
        generator.setStrategy(strategy);
        generator.setTemplateEngine(new VelocityTemplateEngine());
        generator.execute();

    }

}
